<html lang="en-US">

<head>
    
<title>「连载十五」生成二维码、合并海报 - 花落雨忧</title>

<meta property="og:title" content="「连载十五」生成二维码、合并海报 - 花落雨忧">



    



    
    <meta property="description" content="在文章的详情页中，我们常常会需要去宣传它，而目前最常见的就是发海报了，今天我们将实现如下功能：
[&amp;hellip;] 生成二维码
[&amp;hellip;] 合并海报（背景图 &#43; 二维码）
[&amp;hellip;] 首先，你需要在 App 配置项中增加二维码及其海报的存储路径，我们约定配置项名称为 QrCodeSavePath，值为 qrcode/，经过多节连载的你应该能够完成， &amp;hellip;">
    <meta property="og:description" content="在文章的详情页中，我们常常会需要去宣传它，而目前最常见的就是发海报了，今天我们将实现如下功能：
[&amp;hellip;] 生成二维码
[&amp;hellip;] 合并海报（背景图 &#43; 二维码）
[&amp;hellip;] 首先，你需要在 App 配置项中增加二维码及其海报的存储路径，我们约定配置项名称为 QrCodeSavePath，值为 qrcode/，经过多节连载的你应该能够完成， &amp;hellip;">
    






<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1">

<link rel="shortcut icon" href="https://www.lican.site/logo/logo.png" type="image/x-icon" />



<link rel="stylesheet" href="/css/style.min.css" />

<link rel="stylesheet" href="/css/reset.min.css" />




<script src="https://www.lican.site/js/highlight.min.js"></script>

<script>
hljs.configure({ ignoreUnescapedHTML: true })
hljs.highlightAll();
</script>


<script src="https://www.lican.site/js/jquery.min.js"></script>




<link href="https://www.lican.site/css/hugo-code.min.css" rel="stylesheet" />



    <style>
        .post-content img {
            max-width: 400px;
        }
    </style>
</head>

<body id="period" class="home blog">
    <a class="skip-content" href="#main">Press "Enter" to skip to content</a>
    <div id="overflow-container" class="overflow-container">
        <header class="site-header" id="site-header" role="banner">
    <div class="max-width">
        <div id="title-container" class="title-container">
            <div id="site-title" class="site-title"><a href="/">花落雨忧</a></div>
            <p class="tagline">思所及 力所行 方为真.</p>
        </div>
        
        <div id="menu-primary-container" class="menu-primary-container">
            <div class="icon-container">
            </div>
            <div id="menu-primary" class="menu-container menu-primary" role="navigation">
                <nav class="menu">
                    <ul id="menu-primary-items" class="menu-primary-items">
                        
                        
                        <li id="menu-item-0"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/posts" aria-current="page" tabindex="0">首页</a></li>
                        
                        <li id="menu-item-1"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/tech/" aria-current="page" tabindex="1">技术文档</a></li>
                        
                        <li id="menu-item-2"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/article/" aria-current="page" tabindex="2">文章</a></li>
                        
                        <li id="menu-item-3"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/project/" aria-current="page" tabindex="3">项目</a></li>
                        
                        <li id="menu-item-4"
                            class="menu-item menu-item-type-custom menu-item-object-custom ">
                            <a href="/about/" aria-current="page" tabindex="4">关于</a></li>
                        
                    </ul>
                </nav>
            </div>
        </div>
    </div>
</header>
        <div id="primary-container" class="primary-container">
            <div class="max-width">
                <section id="main" class="main" role="main">
                    <div id="loop-container" class="loop-container">
                        <div
                            class="post type-post status-publish format-standard hentry entry">
                            <article>
                                <div class="post-container">
                                    <div class="post-header">
                                        <h2 class="post-title">
                                            <a href="/posts/go/gin/2018-07-05-image/">「连载十五」生成二维码、合并海报</a>
                                        </h2>
                                        
                                        <div class="post-byline">Published on
                                            <a class="date" href="javascript:;">2018/07/05</a>
                                            
                                            
                                            
                                            By 
                                            <a class="author" href="javascript:;">煎鱼</a></div>
                                            
                                        
                                    </div>
                                    <div class="post-content">
                                        <h2 id="知识点">知识点</h2>
<ul>
<li>图片生成</li>
<li>二维码生成</li>
</ul>
<h2 id="本文目标">本文目标</h2>
<p>在文章的详情页中，我们常常会需要去宣传它，而目前最常见的就是发海报了，今天我们将实现如下功能：</p>
<ul>
<li>
<p>生成二维码</p>
</li>
<li>
<p>合并海报（背景图 + 二维码）</p>
</li>
</ul>
<h2 id="实现">实现</h2>
<p>首先，你需要在 App 配置项中增加二维码及其海报的存储路径，我们约定配置项名称为 <code>QrCodeSavePath</code>，值为 <code>qrcode/</code>，经过多节连载的你应该能够完成，若有不懂可参照 <a href="https://github.com/EDDYCJY/go-gin-example/blob/master/conf/app.ini#L14">go-gin-example</a>。</p>
<h2 id="生成二维码">生成二维码</h2>
<h3 id="安装">安装</h3>
<pre><code>$ go get -u github.com/boombuler/barcode
</code></pre><h3 id="工具包">工具包</h3>
<p>考虑生成二维码这一动作贴合工具包的定义，且有公用的可能性，新建 pkg/qrcode/qrcode.go 文件，写入内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">qrcode</span>

<span style="color:#f92672">import</span> (
	<span style="color:#e6db74">&#34;image/jpeg&#34;</span>

	<span style="color:#e6db74">&#34;github.com/boombuler/barcode&#34;</span>
	<span style="color:#e6db74">&#34;github.com/boombuler/barcode/qr&#34;</span>

	<span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/file&#34;</span>
	<span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/setting&#34;</span>
	<span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/util&#34;</span>
)

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">QrCode</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">URL</span>    <span style="color:#66d9ef">string</span>
	<span style="color:#a6e22e">Width</span>  <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">Height</span> <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">Ext</span>    <span style="color:#66d9ef">string</span>
	<span style="color:#a6e22e">Level</span>  <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">ErrorCorrectionLevel</span>
	<span style="color:#a6e22e">Mode</span>   <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">Encoding</span>
}

<span style="color:#66d9ef">const</span> (
	<span style="color:#a6e22e">EXT_JPG</span> = <span style="color:#e6db74">&#34;.jpg&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">NewQrCode</span>(<span style="color:#a6e22e">url</span> <span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">width</span>, <span style="color:#a6e22e">height</span> <span style="color:#66d9ef">int</span>, <span style="color:#a6e22e">level</span> <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">ErrorCorrectionLevel</span>, <span style="color:#a6e22e">mode</span> <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">Encoding</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">QrCode</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">QrCode</span>{
		<span style="color:#a6e22e">URL</span>:    <span style="color:#a6e22e">url</span>,
		<span style="color:#a6e22e">Width</span>:  <span style="color:#a6e22e">width</span>,
		<span style="color:#a6e22e">Height</span>: <span style="color:#a6e22e">height</span>,
		<span style="color:#a6e22e">Level</span>:  <span style="color:#a6e22e">level</span>,
		<span style="color:#a6e22e">Mode</span>:   <span style="color:#a6e22e">mode</span>,
		<span style="color:#a6e22e">Ext</span>:    <span style="color:#a6e22e">EXT_JPG</span>,
	}
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetQrCodePath</span>() <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">AppSetting</span>.<span style="color:#a6e22e">QrCodeSavePath</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetQrCodeFullPath</span>() <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">AppSetting</span>.<span style="color:#a6e22e">RuntimeRootPath</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">AppSetting</span>.<span style="color:#a6e22e">QrCodeSavePath</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetQrCodeFullUrl</span>(<span style="color:#a6e22e">name</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">setting</span>.<span style="color:#a6e22e">AppSetting</span>.<span style="color:#a6e22e">PrefixUrl</span> <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;/&#34;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">GetQrCodePath</span>() <span style="color:#f92672">+</span> <span style="color:#a6e22e">name</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetQrCodeFileName</span>(<span style="color:#a6e22e">value</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">util</span>.<span style="color:#a6e22e">EncodeMD5</span>(<span style="color:#a6e22e">value</span>)
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">q</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">QrCode</span>) <span style="color:#a6e22e">GetQrCodeExt</span>() <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">Ext</span>
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">q</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">QrCode</span>) <span style="color:#a6e22e">CheckEncode</span>(<span style="color:#a6e22e">path</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">bool</span> {
	<span style="color:#a6e22e">src</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">path</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">GetQrCodeFileName</span>(<span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">URL</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">GetQrCodeExt</span>()
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">CheckNotExist</span>(<span style="color:#a6e22e">src</span>) <span style="color:#f92672">==</span> <span style="color:#66d9ef">true</span> {
		<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
	}

	<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">q</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">QrCode</span>) <span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">path</span> <span style="color:#66d9ef">string</span>) (<span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">error</span>) {
	<span style="color:#a6e22e">name</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">GetQrCodeFileName</span>(<span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">URL</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">GetQrCodeExt</span>()
	<span style="color:#a6e22e">src</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">path</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">name</span>
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">CheckNotExist</span>(<span style="color:#a6e22e">src</span>) <span style="color:#f92672">==</span> <span style="color:#66d9ef">true</span> {
		<span style="color:#a6e22e">code</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">URL</span>, <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">Level</span>, <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">Mode</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}

		<span style="color:#a6e22e">code</span>, <span style="color:#a6e22e">err</span> = <span style="color:#a6e22e">barcode</span>.<span style="color:#a6e22e">Scale</span>(<span style="color:#a6e22e">code</span>, <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">Width</span>, <span style="color:#a6e22e">q</span>.<span style="color:#a6e22e">Height</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}

		<span style="color:#a6e22e">f</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">MustOpen</span>(<span style="color:#a6e22e">name</span>, <span style="color:#a6e22e">path</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
		<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">f</span>.<span style="color:#a6e22e">Close</span>()

		<span style="color:#a6e22e">err</span> = <span style="color:#a6e22e">jpeg</span>.<span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">f</span>, <span style="color:#a6e22e">code</span>, <span style="color:#66d9ef">nil</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
	}

	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">name</span>, <span style="color:#a6e22e">path</span>, <span style="color:#66d9ef">nil</span>
}
</code></pre></div><p>这里主要聚焦 <code>func (q *QrCode) Encode</code> 方法，做了如下事情：</p>
<ul>
<li>获取二维码生成路径</li>
<li>创建二维码</li>
<li>缩放二维码到指定大小</li>
<li>新建存放二维码图片的文件</li>
<li>将图像（二维码）以 JPEG 4：2：0 基线格式写入文件</li>
</ul>
<p>另外在 <code>jpeg.Encode(f, code, nil)</code> 中，第三个参数可设置其图像质量，默认值为 75</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#75715e">// DefaultQuality is the default quality encoding parameter.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">const</span> <span style="color:#a6e22e">DefaultQuality</span> = <span style="color:#ae81ff">75</span>

<span style="color:#75715e">// Options are the encoding parameters.
</span><span style="color:#75715e">// Quality ranges from 1 to 100 inclusive, higher is better.
</span><span style="color:#75715e"></span><span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Options</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">Quality</span> <span style="color:#66d9ef">int</span>
}
</code></pre></div><h3 id="路由方法">路由方法</h3>
<p>1、第一步</p>
<p>在 routers/api/v1/article.go 新增 GenerateArticlePoster 方法用于接口开发</p>
<p>2、第二步</p>
<p>在 routers/router.go 的 apiv1 中新增 <code>apiv1.POST(&quot;/articles/poster/generate&quot;, v1.GenerateArticlePoster)</code> 路由</p>
<p>3、第三步</p>
<p>修改 GenerateArticlePoster 方法，编写对应的生成逻辑，如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">const</span> (
	<span style="color:#a6e22e">QRCODE_URL</span> = <span style="color:#e6db74">&#34;https://github.com/EDDYCJY/blog#gin%E7%B3%BB%E5%88%97%E7%9B%AE%E5%BD%95&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GenerateArticlePoster</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
	<span style="color:#a6e22e">appG</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">app</span>.<span style="color:#a6e22e">Gin</span>{<span style="color:#a6e22e">c</span>}
	<span style="color:#a6e22e">qrc</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">NewQrCode</span>(<span style="color:#a6e22e">QRCODE_URL</span>, <span style="color:#ae81ff">300</span>, <span style="color:#ae81ff">300</span>, <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">M</span>, <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">Auto</span>)
	<span style="color:#a6e22e">path</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">GetQrCodeFullPath</span>()
	<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qrc</span>.<span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">path</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#a6e22e">appG</span>.<span style="color:#a6e22e">Response</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR</span>, <span style="color:#66d9ef">nil</span>)
		<span style="color:#66d9ef">return</span>
	}

	<span style="color:#a6e22e">appG</span>.<span style="color:#a6e22e">Response</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>, <span style="color:#66d9ef">nil</span>)
}
</code></pre></div><h3 id="验证">验证</h3>
<p>通过 POST 方法访问 <code>http://127.0.0.1:8000/api/v1/articles/poster/generate?token=$token</code>（注意 $token）</p>
<p><img src="https://s2.ax1x.com/2020/02/15/1xQmb6.jpg" alt="image"></p>
<p>通过检查两个点确定功能是否正常，如下：</p>
<p>1、访问结果是否 200</p>
<p>2、本地目录是否成功生成二维码图片</p>
<p><img src="https://s2.ax1x.com/2020/02/15/1xQCUU.jpg" alt="image"></p>
<h2 id="合并海报">合并海报</h2>
<p>在这一节，将实现二维码图片与背景图合并成新的一张图，可用于常见的宣传海报等业务场景</p>
<h3 id="背景图">背景图</h3>
<p><img src="https://s2.ax1x.com/2020/02/15/1xMXgs.jpg" alt="image"></p>
<p>将背景图另存为 runtime/qrcode/bg.jpg（实际应用，可存在 OSS 或其他地方）</p>
<h3 id="service-方法">service 方法</h3>
<p>打开 service/article_service 目录，新建 article_poster.go 文件，写入内容：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">article_service</span>

<span style="color:#f92672">import</span> (
	<span style="color:#e6db74">&#34;image&#34;</span>
	<span style="color:#e6db74">&#34;image/draw&#34;</span>
	<span style="color:#e6db74">&#34;image/jpeg&#34;</span>
	<span style="color:#e6db74">&#34;os&#34;</span>

	<span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/file&#34;</span>
	<span style="color:#e6db74">&#34;github.com/EDDYCJY/go-gin-example/pkg/qrcode&#34;</span>
)

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">ArticlePoster</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">PosterName</span> <span style="color:#66d9ef">string</span>
	<span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>
	<span style="color:#a6e22e">Qr</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">QrCode</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">NewArticlePoster</span>(<span style="color:#a6e22e">posterName</span> <span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">article</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Article</span>, <span style="color:#a6e22e">qr</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">QrCode</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePoster</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">ArticlePoster</span>{
		<span style="color:#a6e22e">PosterName</span>: <span style="color:#a6e22e">posterName</span>,
		<span style="color:#a6e22e">Article</span>:    <span style="color:#a6e22e">article</span>,
		<span style="color:#a6e22e">Qr</span>:         <span style="color:#a6e22e">qr</span>,
	}
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GetPosterFlag</span>() <span style="color:#66d9ef">string</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;poster&#34;</span>
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">a</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePoster</span>) <span style="color:#a6e22e">CheckMergedImage</span>(<span style="color:#a6e22e">path</span> <span style="color:#66d9ef">string</span>) <span style="color:#66d9ef">bool</span> {
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">CheckNotExist</span>(<span style="color:#a6e22e">path</span><span style="color:#f92672">+</span><span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">PosterName</span>) <span style="color:#f92672">==</span> <span style="color:#66d9ef">true</span> {
		<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">false</span>
	}

	<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">true</span>
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">a</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePoster</span>) <span style="color:#a6e22e">OpenMergedImage</span>(<span style="color:#a6e22e">path</span> <span style="color:#66d9ef">string</span>) (<span style="color:#f92672">*</span><span style="color:#a6e22e">os</span>.<span style="color:#a6e22e">File</span>, <span style="color:#66d9ef">error</span>) {
	<span style="color:#a6e22e">f</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">MustOpen</span>(<span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">PosterName</span>, <span style="color:#a6e22e">path</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>, <span style="color:#a6e22e">err</span>
	}

	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">f</span>, <span style="color:#66d9ef">nil</span>
}

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">ArticlePosterBg</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
	<span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePoster</span>
	<span style="color:#f92672">*</span><span style="color:#a6e22e">Rect</span>
	<span style="color:#f92672">*</span><span style="color:#a6e22e">Pt</span>
}

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Rect</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">Name</span> <span style="color:#66d9ef">string</span>
	<span style="color:#a6e22e">X0</span>   <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">Y0</span>   <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">X1</span>   <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">Y1</span>   <span style="color:#66d9ef">int</span>
}

<span style="color:#66d9ef">type</span> <span style="color:#a6e22e">Pt</span> <span style="color:#66d9ef">struct</span> {
	<span style="color:#a6e22e">X</span> <span style="color:#66d9ef">int</span>
	<span style="color:#a6e22e">Y</span> <span style="color:#66d9ef">int</span>
}

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">NewArticlePosterBg</span>(<span style="color:#a6e22e">name</span> <span style="color:#66d9ef">string</span>, <span style="color:#a6e22e">ap</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePoster</span>, <span style="color:#a6e22e">rect</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Rect</span>, <span style="color:#a6e22e">pt</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">Pt</span>) <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePosterBg</span> {
	<span style="color:#66d9ef">return</span> <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">ArticlePosterBg</span>{
		<span style="color:#a6e22e">Name</span>:          <span style="color:#a6e22e">name</span>,
		<span style="color:#a6e22e">ArticlePoster</span>: <span style="color:#a6e22e">ap</span>,
		<span style="color:#a6e22e">Rect</span>:          <span style="color:#a6e22e">rect</span>,
		<span style="color:#a6e22e">Pt</span>:            <span style="color:#a6e22e">pt</span>,
	}
}

<span style="color:#66d9ef">func</span> (<span style="color:#a6e22e">a</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">ArticlePosterBg</span>) <span style="color:#a6e22e">Generate</span>() (<span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">string</span>, <span style="color:#66d9ef">error</span>) {
	<span style="color:#a6e22e">fullPath</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">GetQrCodeFullPath</span>()
	<span style="color:#a6e22e">fileName</span>, <span style="color:#a6e22e">path</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Qr</span>.<span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">fullPath</span>)
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
	}

	<span style="color:#66d9ef">if</span> !<span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">CheckMergedImage</span>(<span style="color:#a6e22e">path</span>) {
		<span style="color:#a6e22e">mergedF</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">OpenMergedImage</span>(<span style="color:#a6e22e">path</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
		<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">mergedF</span>.<span style="color:#a6e22e">Close</span>()

		<span style="color:#a6e22e">bgF</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">MustOpen</span>(<span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Name</span>, <span style="color:#a6e22e">path</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
		<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">bgF</span>.<span style="color:#a6e22e">Close</span>()

		<span style="color:#a6e22e">qrF</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">file</span>.<span style="color:#a6e22e">MustOpen</span>(<span style="color:#a6e22e">fileName</span>, <span style="color:#a6e22e">path</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
		<span style="color:#66d9ef">defer</span> <span style="color:#a6e22e">qrF</span>.<span style="color:#a6e22e">Close</span>()

		<span style="color:#a6e22e">bgImage</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">jpeg</span>.<span style="color:#a6e22e">Decode</span>(<span style="color:#a6e22e">bgF</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}
		<span style="color:#a6e22e">qrImage</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">jpeg</span>.<span style="color:#a6e22e">Decode</span>(<span style="color:#a6e22e">qrF</span>)
		<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
			<span style="color:#66d9ef">return</span> <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#e6db74">&#34;&#34;</span>, <span style="color:#a6e22e">err</span>
		}

		<span style="color:#a6e22e">jpg</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">image</span>.<span style="color:#a6e22e">NewRGBA</span>(<span style="color:#a6e22e">image</span>.<span style="color:#a6e22e">Rect</span>(<span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Rect</span>.<span style="color:#a6e22e">X0</span>, <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Rect</span>.<span style="color:#a6e22e">Y0</span>, <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Rect</span>.<span style="color:#a6e22e">X1</span>, <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Rect</span>.<span style="color:#a6e22e">Y1</span>))

		<span style="color:#a6e22e">draw</span>.<span style="color:#a6e22e">Draw</span>(<span style="color:#a6e22e">jpg</span>, <span style="color:#a6e22e">jpg</span>.<span style="color:#a6e22e">Bounds</span>(), <span style="color:#a6e22e">bgImage</span>, <span style="color:#a6e22e">bgImage</span>.<span style="color:#a6e22e">Bounds</span>().<span style="color:#a6e22e">Min</span>, <span style="color:#a6e22e">draw</span>.<span style="color:#a6e22e">Over</span>)
		<span style="color:#a6e22e">draw</span>.<span style="color:#a6e22e">Draw</span>(<span style="color:#a6e22e">jpg</span>, <span style="color:#a6e22e">jpg</span>.<span style="color:#a6e22e">Bounds</span>(), <span style="color:#a6e22e">qrImage</span>, <span style="color:#a6e22e">qrImage</span>.<span style="color:#a6e22e">Bounds</span>().<span style="color:#a6e22e">Min</span>.<span style="color:#a6e22e">Sub</span>(<span style="color:#a6e22e">image</span>.<span style="color:#a6e22e">Pt</span>(<span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Pt</span>.<span style="color:#a6e22e">X</span>, <span style="color:#a6e22e">a</span>.<span style="color:#a6e22e">Pt</span>.<span style="color:#a6e22e">Y</span>)), <span style="color:#a6e22e">draw</span>.<span style="color:#a6e22e">Over</span>)

		<span style="color:#a6e22e">jpeg</span>.<span style="color:#a6e22e">Encode</span>(<span style="color:#a6e22e">mergedF</span>, <span style="color:#a6e22e">jpg</span>, <span style="color:#66d9ef">nil</span>)
	}

	<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">fileName</span>, <span style="color:#a6e22e">path</span>, <span style="color:#66d9ef">nil</span>
}
</code></pre></div><p>这里重点留意 <code>func (a *ArticlePosterBg) Generate()</code> 方法，做了如下事情：</p>
<ul>
<li>获取二维码存储路径</li>
<li>生成二维码图像</li>
<li>检查合并后图像（指的是存放合并后的海报）是否存在</li>
<li>若不存在，则生成待合并的图像 mergedF</li>
<li>打开事先存放的背景图 bgF</li>
<li>打开生成的二维码图像 qrF</li>
<li>解码 bgF 和 qrF 返回 image.Image</li>
<li>创建一个新的 RGBA 图像</li>
<li>在 RGBA 图像上绘制 背景图（bgF）</li>
<li>在已绘制背景图的 RGBA 图像上，在指定 Point 上绘制二维码图像（qrF）</li>
<li>将绘制好的 RGBA 图像以 JPEG 4：2：0 基线格式写入合并后的图像文件（mergedF）</li>
</ul>
<h3 id="错误码">错误码</h3>
<p>新增 <a href="https://github.com/EDDYCJY/go-gin-example/blob/master/pkg/e/code.go#L27">错误码</a>，<a href="https://github.com/EDDYCJY/go-gin-example/blob/master/pkg/e/msg.go#L25">错误提示</a></p>
<h3 id="路由方法-1">路由方法</h3>
<p>打开 routers/api/v1/article.go 文件，修改 GenerateArticlePoster 方法，编写最终的业务逻辑（含生成二维码及合并海报），如下：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#66d9ef">const</span> (
	<span style="color:#a6e22e">QRCODE_URL</span> = <span style="color:#e6db74">&#34;https://github.com/EDDYCJY/blog#gin%E7%B3%BB%E5%88%97%E7%9B%AE%E5%BD%95&#34;</span>
)

<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">GenerateArticlePoster</span>(<span style="color:#a6e22e">c</span> <span style="color:#f92672">*</span><span style="color:#a6e22e">gin</span>.<span style="color:#a6e22e">Context</span>) {
	<span style="color:#a6e22e">appG</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">app</span>.<span style="color:#a6e22e">Gin</span>{<span style="color:#a6e22e">c</span>}
	<span style="color:#a6e22e">article</span> <span style="color:#f92672">:=</span> <span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">Article</span>{}
	<span style="color:#a6e22e">qr</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">NewQrCode</span>(<span style="color:#a6e22e">QRCODE_URL</span>, <span style="color:#ae81ff">300</span>, <span style="color:#ae81ff">300</span>, <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">M</span>, <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">Auto</span>) <span style="color:#75715e">// 目前写死 gin 系列路径，可自行增加业务逻辑
</span><span style="color:#75715e"></span>	<span style="color:#a6e22e">posterName</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">GetPosterFlag</span>() <span style="color:#f92672">+</span> <span style="color:#e6db74">&#34;-&#34;</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">GetQrCodeFileName</span>(<span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">URL</span>) <span style="color:#f92672">+</span> <span style="color:#a6e22e">qr</span>.<span style="color:#a6e22e">GetQrCodeExt</span>()
	<span style="color:#a6e22e">articlePoster</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">NewArticlePoster</span>(<span style="color:#a6e22e">posterName</span>, <span style="color:#a6e22e">article</span>, <span style="color:#a6e22e">qr</span>)
	<span style="color:#a6e22e">articlePosterBgService</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">NewArticlePosterBg</span>(
		<span style="color:#e6db74">&#34;bg.jpg&#34;</span>,
		<span style="color:#a6e22e">articlePoster</span>,
		<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">Rect</span>{
			<span style="color:#a6e22e">X0</span>: <span style="color:#ae81ff">0</span>,
			<span style="color:#a6e22e">Y0</span>: <span style="color:#ae81ff">0</span>,
			<span style="color:#a6e22e">X1</span>: <span style="color:#ae81ff">550</span>,
			<span style="color:#a6e22e">Y1</span>: <span style="color:#ae81ff">700</span>,
		},
		<span style="color:#f92672">&amp;</span><span style="color:#a6e22e">article_service</span>.<span style="color:#a6e22e">Pt</span>{
			<span style="color:#a6e22e">X</span>: <span style="color:#ae81ff">125</span>,
			<span style="color:#a6e22e">Y</span>: <span style="color:#ae81ff">298</span>,
		},
	)

	<span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">filePath</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">articlePosterBgService</span>.<span style="color:#a6e22e">Generate</span>()
	<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
		<span style="color:#a6e22e">appG</span>.<span style="color:#a6e22e">Response</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">ERROR_GEN_ARTICLE_POSTER_FAIL</span>, <span style="color:#66d9ef">nil</span>)
		<span style="color:#66d9ef">return</span>
	}

	<span style="color:#a6e22e">appG</span>.<span style="color:#a6e22e">Response</span>(<span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">StatusOK</span>, <span style="color:#a6e22e">e</span>.<span style="color:#a6e22e">SUCCESS</span>, <span style="color:#66d9ef">map</span>[<span style="color:#66d9ef">string</span>]<span style="color:#66d9ef">string</span>{
		<span style="color:#e6db74">&#34;poster_url&#34;</span>:      <span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">GetQrCodeFullUrl</span>(<span style="color:#a6e22e">posterName</span>),
		<span style="color:#e6db74">&#34;poster_save_url&#34;</span>: <span style="color:#a6e22e">filePath</span> <span style="color:#f92672">+</span> <span style="color:#a6e22e">posterName</span>,
	})
}
</code></pre></div><p>这块涉及到大量知识，强烈建议阅读下，如下：</p>
<ul>
<li><a href="https://golang.org/pkg/image/#Rect">image.Rect</a></li>
<li><a href="https://golang.org/pkg/image/#Pt">image.Pt</a></li>
<li><a href="https://golang.org/pkg/image/#NewRGBA">image.NewRGBA</a></li>
<li><a href="https://golang.org/pkg/image/jpeg/#Encode">jpeg.Encode</a></li>
<li><a href="https://golang.org/pkg/image/jpeg/#Decode">jpeg.Decode</a></li>
<li><a href="https://golang.org/pkg/image/draw/#Op">draw.Op</a></li>
<li><a href="https://golang.org/pkg/image/draw/#Draw">draw.Draw</a></li>
<li><a href="https://blog.golang.org/go-imagedraw-package">go-imagedraw-package</a></li>
</ul>
<p>其所涉及、关联的库都建议研究一下</p>
<h3 id="staticfs">StaticFS</h3>
<p>在 routers/router.go 文件，增加如下代码:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#a6e22e">r</span>.<span style="color:#a6e22e">StaticFS</span>(<span style="color:#e6db74">&#34;/qrcode&#34;</span>, <span style="color:#a6e22e">http</span>.<span style="color:#a6e22e">Dir</span>(<span style="color:#a6e22e">qrcode</span>.<span style="color:#a6e22e">GetQrCodeFullPath</span>()))
</code></pre></div><h3 id="验证-1">验证</h3>
<p><img src="https://s2.ax1x.com/2020/02/15/1xMLCQ.jpg" alt="image"></p>
<p>访问完整的 URL 路径，返回合成后的海报并扫除二维码成功则正确 🤓</p>
<p><img src="https://s2.ax1x.com/2020/02/15/1xMhjI.jpg" alt="image"></p>
<h2 id="总结">总结</h2>
<p>在本章节实现了两个很常见的业务功能，分别是生成二维码和合并海报。希望你能够仔细阅读我给出的链接，这块的知识量不少，想要用好图像处理的功能，必须理解对应的思路，举一反三</p>
<p>最后希望对你有所帮助 👌</p>
<h2 id="参考">参考</h2>
<h3 id="本系列示例代码">本系列示例代码</h3>
<ul>
<li><a href="https://github.com/EDDYCJY/go-gin-example">go-gin-example</a></li>
</ul>
<h2 id="关于">关于</h2>
<h3 id="修改记录">修改记录</h3>
<ul>
<li>第一版：2018 年 02 月 16 日发布文章</li>
<li>第二版：2019 年 10 月 02 日修改文章</li>
</ul>
<h2 id="heading">？</h2>
<p>如果有任何疑问或错误，欢迎在 <a href="https://github.com/EDDYCJY/blog">issues</a> 进行提问或给予修正意见，如果喜欢或对你有所帮助，欢迎 Star，对作者是一种鼓励和推进。</p>
<h3 id="我的公众号">我的公众号</h3>
<p><img src="https://image.eddycjy.com/8d0b0c3a11e74efd5fdfd7910257e70b.jpg" alt="image"></p>


                                        
                                        
                                        
                                        <div class="rp4wp-related-posts">
                                            <h3>相关文章</h3>
                                            <ul>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-06-14-excel/">「连载十四」实现导出、导入 Excel</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-06-02-application-redis/">「连载十三」优化你的应用结构和实现Redis缓存</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-05-27-config-upload/">「连载十二」优化配置结构及实现图片上传</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-04-29-cron/">「连载十一」Cron定时任务</a>
                                                    </div>
                                                </li>
                                                
                                                <li>
                                                    <div class="rp4wp-related-post-content">
                                                        <a
                                                            href="https://www.lican.site/posts/go/gin/2018-04-15-gorm-callback/">「连载十」定制 GORM Callbacks</a>
                                                    </div>
                                                </li>
                                                
                                            </ul>
                                        </div>
                                        
                                        
                                    </div>

                                    
                                    
                                    

                                    
                                    <div class="post-meta">
                                        
                                        
                                        <div class="post-tags">
                                            <ul>
                                            
                                            <li>
                                                <a href="/tags/go" title="View all posts tagged match">go</a>
                                            </li>
                                            
                                            <li>
                                                <a href="/tags/gin" title="View all posts tagged match">gin</a>
                                            </li>
                                            
                                            </ul>
                                        </div>
                                        
                                        
                                        <nav class="further-reading">
                                            
                                            <div class="previous">
                                                <span>&lt;&lt; Prev</span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-06-14-excel/"
                                                    rel="prev">「连载十四」实现导出、导入 Excel</a> </div>
                                            
                                            
                                            <div class="next">
                                                <span>Next >></span>
                                                <a href="https://www.lican.site/posts/go/gin/2018-07-07-font/">「连载十六」在图片上绘制文字</a> 
                                            </div>
                                            
                                        </nav>
                                    </div>
                                    
                                    

                                    
                                    
                                    

                                </div>
                            </article>
                        </div>
                    </div>
                </section>
                <aside class="sidebar sidebar-primary" id="sidebar-primary" role="complementary">
    <h1 class="screen-reader-text">Sidebar</h1>
    

    
    
    <section id="text-2" class="widget widget_text">
        <div class="textwidget">
            
            <div id="profile">
                <div id="profile_picture"><img src="https://www.lican.site/logo/logo.png"></div>
                <div id="profile_intro">
                    <p><span class="name">Lican</span></p>
                    <p class="intro">全栈开发者，爱好造轮子。</p>
                </div>
            </div>
            
            <p>
                <script type="text/javascript">
                    (function ($) {
                        $(document).ready(function () {
                            var menuPrimaryContainer = $('#menu-primary-container');
                            var profile = $('#text-2');
                            $('#toggle-navigation').click(function () {
                                if (menuPrimaryContainer.hasClass('open')) {
                                    profile.removeClass('open');
                                } else {
                                    profile.addClass('open');
                                }
                            });
                        });
                    })(jQuery);
                </script>
            </p>
        </div>
    </section>
    
    
    
    
    
    <section id="text-5" class="widget widget_text">
        <h2 class="widget-title">开源项目</h2>
        <div class="textwidget">
            <div id="projects" style="line-height: 22px;">
                
                <a href="https://github.com/idoubi/gonews"
                    target="_blank">gonews</a>: &nbsp;Daily news for golang<br>
                
                <a href="https://github.com/idoubi/sql2struct"
                    target="_blank">sql2struct</a>: &nbsp;Generate go struct according to SQL<br>
                
                <a href="https://github.com/idoubi/goz"
                    target="_blank">goz</a>: &nbsp;Request library used in golang<br>
                
        </div>
    </section>
    
    

    
    
    
    
    

    
    
    

    
    
    

    
    
    
    
</aside>
            </div>
        </div>

        <footer id="site-footer" class="site-footer" role="contentinfo">
    <div class="max-width">
    </div>
    <div class="footer">
        <div id="footercontent">
            © lican.vip All rights reserved<br/>
            Built with Hugo Theme <a href="https://github.com/idoubi/hugo-theme-period" target="_blank">Period</a>
        </div>
    </div>
</footer>

<script>
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?e8351b6d4626d5881d439ea1f6184baa";
      var s = document.getElementsByTagName("script")[0]; 
      s.parentNode.insertBefore(hm, s);
    })();
</script>
    
    
    </div>
    
</body>

</html>